package com.mathdroid.gui.main;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingWorker;

import com.mathdroid.modules.Simulator;

/**
 * This frame is shown while simulating.
 */
public final class SimulatorProgressFrame extends AOptimizerFrame<Void, Void> {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The tabs pane with statistics and combat logs.
	 */
	private final JTabbedPane tabbedPane = new JTabbedPane();
	
	/**
	 * The panel with the statistics summary.
	 */
	private final StatisticsPanel statisticsPanel = new StatisticsPanel(this);
	
	/**
	 * The panel with the sample combat logs.
	 */
	private final CombatLogPanel combatLogPanel = new CombatLogPanel(this);
	
	/**
	 * The preferred width of this panel.
	 */
	private static final int PANEL_WIDTH = 1000;
	
	/**
	 * The preferred height of this panel.
	 */
	private static final int PANEL_HEIGHT = 400;
	
	/**
	 * Create a new progress frame.
	 * @param t SwinWorker task.
	 * @param min Minimum progress value.
	 * @param max Maximum progress value.
	 * @param title Title for the frame.
	 */
	public SimulatorProgressFrame(final SwingWorker<Void, Void> t, 
			final int min, final int max, final String title) {
		super(t, min, max, title);
		
		this.buildPanel();
	}

	/**
	 * Build the panel that is shown after completion.
	 */
	private void buildPanel() {
		JPanel p = this.getContent();
		p.setLayout(new GridLayout(1, 1));
		p.add(this.tabbedPane);
		this.tabbedPane.addTab(
				"Summary (Average Values)", this.statisticsPanel);
		this.tabbedPane.addTab("Combatlog Samples", this.combatLogPanel);
		this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
	}

	@Override
	protected void done(final Void result) {
		Toolkit.getDefaultToolkit().beep();
		
		Simulator s = (Simulator) this.getTask();
		this.statisticsPanel.setSimulationStatistics(s.getStatistics());
		this.combatLogPanel.setCombatLogs(s.getCombatLogs());
	}
}
